/**
 * 
 */
package com.acc.softmgr.business.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acc.softmgr.business.model.SoftmgrCategory;
import com.acc.softmgr.business.model.SoftmgrCategoryExample;
import com.acc.softmgr.business.model.SysUser;
import com.acc.softmgr.business.service.ISoftmgrCategoryService;
import com.acc.softmgr.common.constant.DeleteStatus;
import com.acc.softmgr.common.constant.enums.RestStatus;
import com.acc.softmgr.common.utils.date.DateBase;
import com.acc.softmgr.common.utils.string.StringUtil;
import com.acc.softmgr.common.web.DataGridObject;
import com.acc.softmgr.common.web.ResponseObject;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @content
 * 
 * @date 2017年5月8日
 * 
 * @author OprCalf
 * 
 */
@Controller
@RequestMapping("softCategory")
public class SoftmgrCategoryController {

	@Autowired
	private ISoftmgrCategoryService softmgrCategoryService;

	@RequestMapping(value = "toSoftmgrCategory", method = RequestMethod.GET)
	public String toSoftmgrCategory() {
		return "softCategory/softCategory";
	}

	/**
	 * 查询列表页
	 * 
	 * @param project_name
	 * @param project_english
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchSoftmgrCategory", method = RequestMethod.GET)
	@ResponseBody
	public DataGridObject<SoftmgrCategory> searchSoftmgrCategory(@Validated Integer searchCategoryId, Integer page, Integer rows,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		DataGridObject<SoftmgrCategory> responseObject = new DataGridObject<SoftmgrCategory>();
		SoftmgrCategoryExample example = new SoftmgrCategoryExample();
		if (searchCategoryId != null) {
			example.getCriteria().andIdEqualTo(searchCategoryId);
		}
		if (page == null || page == 0) {
			page = 1;
		}

		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		example.setCount(page * rows);
		example.setLimitStart((page - 1) * rows);

		List<SoftmgrCategory> SoftmgrCategorys = softmgrCategoryService.selectByExample(example);
		for (SoftmgrCategory SoftmgrCategory : SoftmgrCategorys) {
			SoftmgrCategory.setCreateTimestring(
					DateBase.formatDate(SoftmgrCategory.getCreatetime(), DateBase.DATE_PATTERN_DATETIME));
		}
		responseObject.setTotal(page * rows);
		responseObject.setRows(SoftmgrCategorys);
		return responseObject;
	}

	/**
	 * 新增项目
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addSoftmgrCategory", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject addSoftmgrCategory(@Validated String category_name, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (StringUtil.isEmpty(category_name)) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SysUser currentUser = (SysUser) request.getSession().getAttribute("currentUser");

		SoftmgrCategory SoftmgrCategory = new SoftmgrCategory();
		SoftmgrCategory.setCreatetime(new Date());
		SoftmgrCategory.setCreateby(currentUser.getUsername());
		SoftmgrCategory.setCategory_name(category_name);
		SoftmgrCategory.setDeleted(DeleteStatus.FALSE);
		// -------------------
		SoftmgrCategoryExample example = new SoftmgrCategoryExample();
		example.getCriteria().andCategory_nameEqualTo(category_name);
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		List<SoftmgrCategory> SoftmgrCategorys = softmgrCategoryService.selectByExample(example);
		if (SoftmgrCategorys != null && SoftmgrCategorys.size() > 0) {
			responseObject.setStatus(RestStatus.NOT_UNIQUE.code);
			responseObject.setMessage(RestStatus.NOT_UNIQUE.message);
			return responseObject;
		}
		// ----------判断唯一性------------
		softmgrCategoryService.insert(SoftmgrCategory);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 修改用户
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "editSoftmgrCategory", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject editSoftmgrCategory(@Validated Integer id, String category_name, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (StringUtil.isEmpty(category_name) || id == null || id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SoftmgrCategory SoftmgrCategory = softmgrCategoryService.selectByPrimaryKey(id);
		boolean exitSoftmgrCategory = exitSoftmgrCategory(category_name);
		if (exitSoftmgrCategory) {
			responseObject.setStatus(RestStatus.NOT_UNIQUE.code);
			responseObject.setMessage(RestStatus.NOT_UNIQUE.message);
			return responseObject;
		}
		// ----------判断唯一性------------
		SoftmgrCategory.setCategory_name(category_name);
		softmgrCategoryService.updateByPrimaryKey(SoftmgrCategory);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 查询详情
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{id}/searchSoftmgrCategory", method = RequestMethod.GET)
	@ResponseBody
	public ResponseObject searchUser(@PathVariable Integer id) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (id == null || id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SoftmgrCategory SoftmgrCategory = softmgrCategoryService.selectByPrimaryKey(id);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		responseObject.setData(SoftmgrCategory);
		return responseObject;
	}

	/**
	 * 删除项目
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/removeSoftmgrCategory", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject removeSoftmgrCategory(@Validated Integer id) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (id == null || id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SoftmgrCategory SoftmgrCategory = softmgrCategoryService.selectByPrimaryKey(id);
		SoftmgrCategory.setDeleted(DeleteStatus.TRUE);
		softmgrCategoryService.updateByPrimaryKey(SoftmgrCategory);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 获取所有Category项目
	 * 
	 * @return
	 */
	@RequestMapping(value = "getSoftmgrCategorys", method = RequestMethod.GET)
	@ResponseBody
	public String getSoftmgrCategorys() throws Exception {

		SoftmgrCategoryExample example = new SoftmgrCategoryExample();
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);

		List<SoftmgrCategory> list = softmgrCategoryService.selectByExample(example);
		JSONArray jsonArray = new JSONArray();
		for (SoftmgrCategory category : list) {
			JSONObject json = new JSONObject();
			json.put("id", category.getId());
			json.put("text", category.getCategory_name());
			jsonArray.add(json);
		}

		return jsonArray.toJSONString();
	}

	private boolean exitSoftmgrCategory(String category_name) {
		SoftmgrCategoryExample example = new SoftmgrCategoryExample();
		example.getCriteria().andCategory_nameEqualTo(category_name);
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		List<SoftmgrCategory> SoftmgrCategorys = softmgrCategoryService.selectByExample(example);
		if (SoftmgrCategorys != null && SoftmgrCategorys.size() > 0) {
			return true;
		} else {
			return false;
		}
	}
}
